import { ref } from 'vue'

import { getUserDetail, sendUserSign } from './api'
import { catchFn } from '@/libs/common'
import { updateUserInfo } from '@/libs/user'

/** 用户详情 */
const userDetail = ref<T_UserDetail>()

export { userDetail }

/**
 * 更新用户详情
 */
const updateUserDetail = () =>
  catchFn(async () => {
    const { data } = await getUserDetail()

    userDetail.value = data
  })

/**
 * 签到
 */
const sign = () =>
  catchFn(async () => {
    await sendUserSign()

    const { data } = await getUserDetail()

    userDetail.value = data

    updateUserInfo()
  })

/**
 * 清空
 */
const clear = () => (userDetail.value = undefined)

export { updateUserDetail, sign, clear }
